/**
 * Notes: 足迹模块业务逻辑
 * Ver : CCMiniCloud Framework 2.0.1 ALL RIGHTS RESERVED BY www.code942.com
 * Date: 2025-11-14 07:48:00
 */

const BaseBiz = require('./base_biz.js');
const api = require('@/api/api.js');
const PassportBiz = require('@/utils/comm/biz/passport_biz.js');
const pageHelper = require('@/utils/helper/page_helper.js');
// const CACHE_FOOT = 'CACHE_FOOT';
class FootBiz extends BaseBiz {
    static getFootList() {
        let foot = cacheHelper.get(CACHE_FOOT);
        if (foot) {
            for (let i = 0; i < foot.length; i++) {
                foot[i].time = timeHelper.timestamp2Time(foot[i].time);
            }
        }
        return foot;
    }

    /**添加足迹缓存
     *
     * @param {*} key 键
     * @param {*} val 值
     * 格式 key:{
     *  type:类型
     *  title:标题
     *  time:加入时间
     * }
     * @param {*} size 最大个数
     * @param {*} expire 过期时间
     */
    static addFoot(id,type, title, size = 60, expire = 94608000) {
        let path = pageHelper.getCurrentPageUrlWithArgs();
        if (!path || !title || !type) {
            return [];
        }   
        let foot = cacheHelper.get(CACHE_FOOT, []);
        // 查询是否存在 并删除
        for (let k = 0; k < foot.length; k++) {
            if (id == foot[k].id) {
                foot.splice(k, 1);
            }
        }
        // 加到头部
        let val = {
            id,
            path,
            type,
            title,
            time: timeHelper.time()
        };
        foot.unshift(val);

        // 判断个数， 多的删除
        if (foot.length > size) {
            foot.splice(foot.length - 1, 1);
        }

        // 存缓存
        cacheHelper.set(CACHE_FOOT, foot, expire);
        return foot;
    }

    static async addFootNew(id,type, title){
        //调用远程接口 添加足迹
        // let foot = cacheHelper.get(CACHE_FOOT, []);
        //title 可以为空
        let path = pageHelper.getCurrentPageUrlWithArgs();
        if (!path  || !type || !id) {
            return ;
        }   
        let params = {
            relatedId:id,
            relatedType:type,
            title:title || '',
            path:path,
        }
        var result = await PassportBiz.isLogin();
        if(result){
            let res = await api.addFoot(params);
            if(res.code == 200){
                return true;
            }else{
                return false;
            }
        }
    }
}
module.exports = FootBiz;
